//
//  Share.m
//  CYWFrame
//
//  Created by Macx on 2017/11/3.
//  Copyright © 2017年 Macx. All rights reserved.
//

#import "Share.h"

//@class ShareSDK;
//#import <ShareSDK/NSMutableDictionary+SSDKShare.h>
#import <ShareSDK/ShareSDK.h>
#import <ShareSDKUI/ShareSDK+SSUI.h>

@implementation Share


+ (void)shareActionwx:(NSDictionary *)param {
    
    NSLog(@"分享参数 %@", param);
    
    NSMutableDictionary *shareParams = [NSMutableDictionary dictionary];

    NSString *text = param[@"text"];
    
    if (text.length > 20) {
        text = [text substringToIndex:20];
    }

    
    [shareParams SSDKSetupShareParamsByText:text
                                     images:param[@"img"]
                                        url:[NSURL URLWithString:param[@"url"]]
                                      title:param[@"title"]
                                       type:SSDKContentTypeAuto];
    [self share:shareParams platform:SSDKPlatformSubTypeWechatSession];
    
}
+ (void)shareActionpyq:(NSDictionary *)param {
    
    NSMutableDictionary *shareParams = [NSMutableDictionary dictionary];
    
    NSString *text = param[@"text"];
    if (text.length > 20) {
        text = [text substringToIndex:20];
    }
    [shareParams SSDKSetupShareParamsByText:text
                                     images:param[@"img"]
                                        url:[NSURL URLWithString:param[@"url"]]
                                      title:param[@"text"]
                                       type:SSDKContentTypeAuto];
    [self share:shareParams platform:SSDKPlatformSubTypeWechatTimeline];
}
+ (void)shareActionqq:(NSDictionary *)param {
    
    NSString *text = param[@"text"];
    if (text.length > 20) {
        text = [text substringToIndex:20];
    }
    NSMutableDictionary *shareParams = [NSMutableDictionary dictionary];
    [shareParams SSDKSetupShareParamsByText:text
                                     images:param[@"img"]
                                        url:[NSURL URLWithString:param[@"url"]]
                                      title:param[@"title"]
                                       type:SSDKContentTypeAuto];
    [self share:shareParams platform:SSDKPlatformSubTypeQQFriend];
    
}
+ (void)shareActionqqZone:(NSDictionary *)param {
    
    NSString *text = param[@"text"];
    if (text.length > 20) {
        text = [text substringToIndex:20];
    }
    NSMutableDictionary *shareParams = [NSMutableDictionary dictionary];
    [shareParams SSDKSetupShareParamsByText:text
                                     images:param[@"img"]
                                        url:[NSURL URLWithString:param[@"url"]]
                                      title:param[@"title"]
                                       type:SSDKContentTypeAuto];
    [self share:shareParams platform:SSDKPlatformSubTypeQZone];
    
}
+ (void)shareActionwb:(NSDictionary *)param {
    
    
    NSString *text = param[@"text"];
    if (text.length > 100) {
        text = [text substringToIndex:100];
    }
    
    NSMutableDictionary *shareParams = [NSMutableDictionary dictionary];
    [shareParams SSDKEnableUseClientShare];
    [shareParams SSDKSetupShareParamsByText:[NSString stringWithFormat:@"%@...%@", text, param[@"url"]]
                                     images:param[@"img"]
                                        url:nil
                                      title:param[@"title"]
                                       type:SSDKContentTypeAuto];
    [self share:shareParams platform:SSDKPlatformTypeSinaWeibo];
    
}

+ (void)share:(NSMutableDictionary *)param platform:(NSUInteger)platform {
    
    [ShareSDK share:platform parameters:param onStateChanged:^(SSDKResponseState state, NSDictionary *userData, SSDKContentEntity *contentEntity, NSError *error) {
        switch (state) {
            case SSDKResponseStateSuccess:
            {
                UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"分享成功"
                                                                    message:nil
                                                                   delegate:nil
                                                          cancelButtonTitle:@"确定"
                                                          otherButtonTitles:nil];
                [alertView show];
                break;
            }
            case SSDKResponseStateFail:
            {
                UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"分享失败"
                                                                message:[NSString stringWithFormat:@"%@",error]
                                                               delegate:nil
                                                      cancelButtonTitle:@"OK"
                                                      otherButtonTitles:nil, nil];
                [alert show];
                break;
            }
            default:
                break;
        }
    }];
}

+ (void)shareAction:(NSDictionary *)param {
    
    // param ： title text url
    
    //1、创建分享参数
    //（注意：图片必须要在Xcode左边目录里面，名称必须要传正确，如果要分享网络图片，可以这样传iamge参数 images:@[@"http://mob.com/Assets/images/logo.png?v=20150320"]）
    NSMutableDictionary *shareParams = [NSMutableDictionary dictionary];
    
    //        [shareParams SSDKSetupWeChatParamsByText:@"分享文章" title:self.titleName url:[NSURL URLWithString:self.url] thumbImage:self.img image:nil musicFileURL:nil extInfo:nil fileData:nil emoticonData:nil type:SSDKContentTypeAuto forPlatformSubType:SSDKPlatformSubTypeWechatTimeline];
    //   分享（可以弹出我们的分享菜单和编辑界面）
    
    [shareParams SSDKSetupShareParamsByText:param[@"text"]
                                     images:param[@"img"]
                                        url:[NSURL URLWithString:param[@"url"]]
                                      title:param[@"title"]
                                       type:SSDKContentTypeAuto];
    
    [ShareSDK showShareActionSheet:nil //要显示菜单的视图, iPad版中此参数作为弹出菜单的参照视图，只有传这个才可以弹出我们的分享菜单，可以传分享的按钮对象或者自己创建小的view 对象，iPhone可以传nil不会影响
                             items:@[@(SSDKPlatformSubTypeWechatSession),@(SSDKPlatformSubTypeQQFriend), @(SSDKPlatformSubTypeWechatTimeline)]
                       shareParams:shareParams
               onShareStateChanged:^(SSDKResponseState state, SSDKPlatformType platformType, NSDictionary *userData, SSDKContentEntity *contentEntity, NSError *error, BOOL end) {
                   
                   switch (state) {
                       case SSDKResponseStateSuccess:
                       {
                           UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"分享成功"
                                                                               message:nil
                                                                              delegate:nil
                                                                     cancelButtonTitle:@"确定"
                                                                     otherButtonTitles:nil];
                           [alertView show];
                           break;
                       }
                       case SSDKResponseStateFail:
                       {
                           UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"分享失败"
                                                                           message:[NSString stringWithFormat:@"%@",error]
                                                                          delegate:nil
                                                                 cancelButtonTitle:@"OK"
                                                                 otherButtonTitles:nil, nil];
                           [alert show];
                           break;
                       }
                       default:
                           break;
                   }
               }];
    

}

@end
